package edu.jiangnan.crm.workbench.service.Impl;

import edu.jiangnan.crm.commons.contant.Contants;
import edu.jiangnan.crm.commons.utils.DateUtils;
import edu.jiangnan.crm.commons.utils.UUIDUtils;
import edu.jiangnan.crm.settings.domain.User;
import edu.jiangnan.crm.workbench.domain.Clue;
import edu.jiangnan.crm.workbench.domain.Customer;
import edu.jiangnan.crm.workbench.mapper.ClueMapper;
import edu.jiangnan.crm.workbench.mapper.CustomerMapper;
import edu.jiangnan.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("ClueService")
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public int saveCreateClue(Clue clue) {
        return clueMapper.insertClue(clue);
    }

    @Override
    public Clue queryClueForDetailById(String id) {
        return clueMapper.selectClueForDetailById(id);
    }

    @Override
    public void saveConvert(Map<String, Object> map) {
        String clueId =(String)map.get("clueId");
        User user = (User)map.get(Contants.SESSION_USER);

        Clue clue = clueMapper.selectClueById(clueId);

        Customer c=new Customer();
        c.setAddress(clue.getAddress());
        c.setContactSummary(clue.getContactSummary());
        c.setCreateBy(user.getId());
        c.setCreateTime(DateUtils.formatDateTime(new Date()));
        c.setDescription(clue.getDescription());
        c.setId(UUIDUtils.getUUID());
        c.setName(clue.getCompany());
        c.setNextContactTime(clue.getNextContactTime());
        c.setOwner(user.getId());
        c.setPhone(clue.getPhone());
        c.setWebsite(clue.getWebsite());
        customerMapper.insertCustomer(c);
    }
}
